/**

 @Name：layuiAdmin 内容系统
 @Author：star1029

 @License：LPPL

 */


layui.define(['table', 'form'], function(exports){
  var $ = layui.$
  ,table = layui.table
  ,form = layui.form;

  //文章管理
  table.render({
    elem: '#LAY-app-content-list'
    ,url: ARTICLE_LIST_URL //模拟接口
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'id', width: 90, title: 'ID', sort: false}
      ,{field: 'sort', title: '排序', templet: '#sortTpl',width: 120}
      ,{field: 'title', title: '标题',templet: '#titleTpl', minWidth: 100}
      ,{field: 'category_title',  title:'所属栏目', width: 180}
      ,{field: 'updated_at',  title:'修改时间', width: 170}
      ,{field: 'status', title: '状态', templet: '#buttonTpl', width: 80, align: 'center'}
      ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-content-list'}
    ]]
    ,page: true
    ,limit: 15
    ,limits: [10, 15, 20, 25, 30]
    ,text: '对不起，加载出现异常！'
    ,parseData: function(res){
      if (res['count'] === 0){
        return {
          'code': 201,
          'msg': '暂无记录',
          'count': 0,
          'data': []
        }
      }
    }
  });

  //监听工具条
  table.on('tool(LAY-app-content-list)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除此文档？', function(index){
        var field = data.field; //获取提交的字段
        $.ajax({
          method: 'post',
          data: {"id": data.id},
          url: ARTICLE_DELETE_URL,
          success: function (res) {
            if (res.result.status) {
              layer.msg(res.result.msg, {
                offset: '15px'
                , icon: 2
                , time: 1000
              });
              return
            }
            obj.del();
            layui.table.reload('LAY-app-content-list'); //重载表格
            form.render();
            layer.close(index);
          }
        })
      });
    } else if(obj.event === 'edit'){
      location.href = ARTICLE_EDIT_URL + '&id=' + data.id
    }
  });


  exports('article', {})
});